import * as api from '@/server/getData.js'
import * as utils from '@/utils/index.js'
// 判断是否uniapp内核浏览器 app端
export const isUniApp = {
	data(){
		return {
			isUniApp:false,
		}
	},
	methods:{
		get_isUniApp(){
			const ua = navigator.userAgent.toLowerCase()
			this.isUniApp = ua.match(/uni-app/i) == "uni-app"?true:false
		}
	}
}
// 加载页面
export const loadPage = {
	data(){
		return {
			loadPage:true, //显示是否加载
			loadingMsg:'资源加载中...' //加载中的提示语
		}
	},
	methods:{
		update_loadingMsg(msg){
			if(typeof msg !== 'string'){
				console.log(`传入的参数必须为字符串类型`)
				return
			}
			this.loadingMsg = msg
		}
	}
}
// 锁竖屏幕
export const lockScreen = {
	methods:{
		startPlusReady(){
			try{
				if (window.plus) {
					// 在这里调用5+ API
					this.plusReady();
				} else { 
					// 兼容老版本的plusready事件
					document.addEventListener('plusready', this.plusReady(), false);
				}
			}catch(e){
				//TODO handle the exception
			}
		},
		plusReady(){
			try{
				plus.screen.lockOrientation('portrait-primary');
			}catch(e){
				//TODO handle the exception
			}
		}
	}
}
// 获取配置信息
export const pzInfo = {
	data(){
		return {
			pzInfo:null,
		}
	},
	methods:{
		async get_peizhi(){
			const res = await api.common.peizhi()
			this.pzInfo = res
		}
	}
}
// 登录跳转
export const navToLogin = {
	methods:{
		navToLogin(){
			const _this = this
			uni.showModal({
				title: '提示',
				content: '需要登录才能使用',
				cancelText: '以后再说',
				confirmText: '前往登录',
				success: function (res) {
					if (res.confirm) {
						h5uni.redirectTo({url:`/pages/user/back_login?url=${_this.pzInfo.share.appLink}`})
					} else if (res.cancel) {
						h5uni.switchTab({ url: '/pages/index/index' })
					}
				}
			})
		}
	}
}
// app分享
export const appchat = {
	methods:{
		async get_app_init(params={}){
			const { needShare,strShareId, strClassId,strClassName,title,desc,imgUrl,link } = params
			if(!needShare || !strShareId || !strClassId || !strClassName || !title || !desc || !imgUrl || !link){
				throw('app分享参数没传。needShare,strShareId,strClassId,strClassName,标题title,描述desc,分享图imgUrl,链接link')
				return
			}
			const optionsTmp = needShare?{
				'share':{ 
					strShareId, 
					strClassId, 
					strClassName, 
					title, 
					'thumb':imgUrl, 
					'summary': desc, 
					'url':`${link}?time=${new Date().getTime()}` 
				}
			}:{ 'unshare':true }
			h5uni.postMessage({
				data: {
					...optionsTmp,
					'Lock':true
				}
			})
		}
	}
}
// 微信分享
export const wechat = {
	methods:{
		async get_wx_init(params = {}){
			const { title,desc,imgUrl,link } = params
			if(!title || !desc || !imgUrl || !link){
				throw('微信分享参数没传。标题title,描述desc,分享图imgUrl,链接link')
				return
			}
			const urlData = {
				url: utils.wechat.formatLink(document.location.href.split('#')[0])
			}
			const res = await api.weixin.getConfig(urlData)
			const data = JSON.parse(res)
			utils.wechat.configInit({
				appId:data.appId,
				timestamp:data.timestamp,
				nonceStr:data.nonceStr,
				signature:data.signature,
			})
			utils.wechat.readyInit({ title,desc,imgUrl,link })
			// utils.wechat.readyInit({
			// 	title: this.pzInfo.wx.wxShareTitle,
			// 	desc: this.pzInfo.wx.wxShareDec,
			// 	imgUrl: this.pzInfo.wx.wxShareThumb,
			// 	link:`${this.pzInfo.wx.wxLink}?time=${new Date().getTime()}`
			// })
		}
	}
}